import Vue from 'vue'
import VueRouter from 'vue-router'
import { Loading } from "element-ui";
import { loadingUtils } from '@/js/loadUtils';
import { user } from '@/store/storeTest';

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'index',
        component: () => import('@/views/Home.vue'),
        meta: { check: true }
    },
    {
        path: '/404',
        name: '404',
        component: () => import("@/views/404.vue"),
        meta: { check: false }
    },
    {
        path: "/login",
        name: 'login',
        component: () => import("@/views/Login.vue"),
        meta: { check: false }
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if(to.meta.check == null) { router.push("404"); } 
    loadingUtils.close();
    next();
    // 如果要check, 到服务器验证, 更新user信息, 如果没有跳转到登录页面
    // if (to.meta && to.meta.check) {
    //     fetchPost("/frontUser/checkSession")
    //         .then((res) => {
    //             store.commit("setUser", res.data);
    //             next();
    //         })
    //         .catch(() => {
    //             router.push("/login");
    //         })
    // } else {
    //     next();
    // }
});

export default router
